<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminLog;
use App\Model\CheckRecharage;
use App\Model\OperationLogs;
use App\Model\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 * Created by PhpStorm.
 * User: wangjiangtao
 * Date: 2021/11/12
 * Time: 10:29
 */
class CheckRecharageController extends BaseController
{
    protected $model = null;

    public function __construct(Request $request,CheckRecharage $checkRecharage)
    {
        parent::__construct($request);
        $this->model = $checkRecharage;
    }

    public function getList(Request $request){
        return $this->toJson(self::SUCCESS,$this->model->getList($request));
    }

    public function getRule(){
        return $this->toJson(self::SUCCESS,$this->model->getRule());
    }

    public function updateRecharage(Request $request){
        $validator = Validator::make($request->all(),[
            'id'    => 'required|int',
            'user_id'   => 'required|int',
            'rule'   => 'required|int',
//            'order_id'  => 'required|string',
        ],[
            'user_id.required'   => '用户id不可为空',
            'id.required'   => 'id不可为空',
            'rule.required'   => '规则不可为空',
            'recharge_img.required'   => '转账凭证不可为空',
            'user_id.int'   => '用户id不合法',
            'id.int'   => 'id不合法',
            'rule.int'   => '规则错误',
//            'order_id.required'   => '转账凭证id不可为空',
//            'order_id.string'   => '转账凭证id不合法',
        ]);
        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }
        $data = $this->model->getdataByid($request->input('id'));
        if (!$data || $data['status'] != 0 ){
            return $this->toJson(self::ERROR, [],'数据不存在或不可更改');
        }
        if ($request->input('order_id')){
            $orderdata = $this->model->getdataByOrderid($request->input('order_id'));
            if ($orderdata && $orderdata['id'] != $data['id']){
                return $this->toJson(self::ERROR, [],'订单序号已存在');
            }
        }
        $res = $this->model->updateRecharage($request->only(['id','rule','order_id','remark']));
        if (!$res || $res['code'] != 0){
            return $this->toJson(self::ERROR,[],$res['msg']);
        }
        OperationLogs::createData($request,OperationLogs::FINANCE, '充值转账编辑', $request->input('id'));
        return $this->toJson(self::SUCCESS);

    }
    public function checkData(Request $request){
        $validator = Validator::make($request->all(),[
            'id'    => 'required|int',
            'status'   => 'required|int',
        ],[
            'id.required'   => 'id不可为空',
            'id.int'   => 'id不合法',
            'status.required'   => 'status不可为空',
            'status.int'   => 'status不合法',
        ]);
        if($validator->fails()){
            return $this->toJson(self::PARAM_ERROR, [],$validator->errors()->all()[0]);
        }
        $data = $this->model->getdataByid($request->input('id'));
        if (!$data || $data['status'] != 0 ){
            return $this->toJson(self::ERROR, [],'数据不存在或不可更改');
        }
        if (!$data['order_id'] && $request->input('status') ==1){
            return $this->toJson(self::ERROR, [],'订单序号不可为空');
        }
        $data = $request->only(['id','status']);
        $token = $request->header('x-token') ?? '';
        $users = Users::findByToken($token);
        $data['opt_id'] = $users->id;
        $data['updated_at'] = time();
        $res = $this->model->updateRecharage($data);
        if (!$res || $res['code'] != 0){
            return $this->toJson(self::ERROR,[],$res['msg']);
        }
        OperationLogs::createData($request,OperationLogs::FINANCE, '充值转账审核', $request->input('id'));
        return $this->toJson(self::SUCCESS);
    }

}
